# network
from Cube.network.Utility import getIPList
from GameLobbyBase import GameLobbyBase
# cmd
from Cube.command.QCommand import QCmdClientSetup
# event
from Cube.command.QCmdInputPipe.EventDefine import event_QSERVERMODEPIPE_NEWCLIENTJOIN
# memType
from Cube.map import QMemberType

class GameLobbyServer(GameLobbyBase):
    def __init__(self):
        GameLobbyBase.__init__(self)
        self._MapStructureInit()
        # track client's LinkID for CmdMemberLeave
        self.ClientMemberID2LinkIDMap = self.CmdInputPipe.MemberID2LinkIDMapGet()
        # command.Send
        self.CmdClientSetupSend = QCmdClientSetup().Send
        # event handle
        self.accept(event_QSERVERMODEPIPE_NEWCLIENTJOIN, self.NewClientSetupHandle)

        self._ServerSetup()

    def IPAddressGet(self):
        ipAddress = getIPList()
        return ipAddress

    # internal function
    def _ServerSetup(self):
        memberID = self._ServerMemberIDAssign()
        self._MemberJoinCmdSend(memberID)  # send member join cmd

    def _ServerMemberIDAssign(self):
        memberID = self.MapHeader.ServerMemberIDGet()
        if memberID is None:
            memberID = self._FirstValidMemberIDGet()
        self._MyMemberIDSet(memberID)
        return memberID

    # callback
    def NewClientSetupHandle(self, LinkID):
        mapNamem = self.MapHeader.MapInfoGet().Name
        checksum = 0   # TODO...
        clientMemberID = self._FirstValidMemberIDGet()
        syncData = self._SyncDataGet()
        self.CmdClientSetupSend(mapNamem, checksum, clientMemberID, syncData, LinkID)
        self.ClientMemberID2LinkIDMap[clientMemberID] = LinkID

    def _SyncDataGet(self):
        # parameters of MemberJoinHandle at client
        retMemberList = [ [memberEntry.MemberName,
                           memberEntry.CurrentMemberType,
                           memberEntry.MemberID]
                         for memberEntry in self.MemberListMgr.MemberListGet()]
        randomSeed = self.MapHeader.RandomSeedGet()
        ret = {"memberList":retMemberList,
               "randomSeed":randomSeed}
        return ret

    def MemberJoinHandle(self, memberName, memberType, memberID):
        memberEntry = self.MemberListMgr.ID2MemberGet(memberID)
        oldMemberType = memberEntry.CurrentMemberType
        GameLobbyBase.MemberJoinHandle(self, memberName, memberType, memberID)
        if (oldMemberType == QMemberType.OPEN) and \
           (memberType in [QMemberType.CLOSE, QMemberType.COMPUTER]):
            self.CmdInputPipe.SlotClose()
        elif (oldMemberType in [QMemberType.CLOSE, QMemberType.COMPUTER]) and \
             (memberType == QMemberType.OPEN):
            self.CmdInputPipe.SlotOpne()

    def MemberLeaveHandle(self, memberID):
        GameLobbyBase.MemberLeaveHandle(self, memberID)
        # remove connection of leaved client
        self.CmdInputPipe.LinkRemove(self.ClientMemberID2LinkIDMap[memberID])

    def MemberMoveHandle(self, memberID, move2TeamID):
        newMemberID = GameLobbyBase.MemberMoveHandle(self, memberID, move2TeamID)
        if self.ClientMemberID2LinkIDMap.HasIDA(memberID):
            self.ClientMemberID2LinkIDMap.IDAChange(oldIDA = memberID,
                                                    newIDA = newMemberID)

    def MemberAssignHandle(self, memberID, newMemberType):
        memberEntry = self.MemberListMgr.ID2MemberGet(memberID)
        currMemberType = memberEntry.CurrentMemberType
        if currMemberType != newMemberType:
            if currMemberType == QMemberType.HUMAN:
                self.CmdMemberLeaveSend(memID = memberID)

            self.CmdMemberJoinSend(memberName = "",
                                   memberType = newMemberType,
                                   memID = memberID
                                   )
